package com.cloudptt.wp.ui.more.advanced;

import android.os.Bundle;
import android.preference.EditTextPreference;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceChangeListener;
import android.preference.Preference.OnPreferenceClickListener;
import android.preference.SwitchPreference;
import android.text.InputType;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Switch;
import android.widget.TextView;

import com.cloudptt.api.comm.CheckData;
import com.cloudptt.api.product.Product;
import com.cloudptt.api.product.config.lbs.LocationConfig;
import com.cloudptt.api.product.log.Log;
import com.cloudptt.wp.R;
import com.cloudptt.wp.SysApplication;
import com.cloudptt.wp.ui.BasePreferenceActivity;

public class CustomPositioningActivity extends BasePreferenceActivity implements OnPreferenceChangeListener, OnPreferenceClickListener{
	
	public static final String THIS_FILE = "CustomPositioningActivity";
	
//	private SharedPreferences sp;
	
	private Switch isOpenLocationwitch;
//	private Switch server_switch;
//	private TextView server_ip;
//	private View ly_mode;
	private TextView timeIntervalTextView;
	private TextView distanceIntervalTextView;
//	private View ly_server;
//	private TextView tv_positioning_server_ipport;
	
	private TextView back;

//	private	RadioGroup  rg_position_choice;	
//	private RadioButton rd_sub;
//	private RadioButton rd_all;
	
	private RadioGroup  rg_position_map;
	private RadioButton rd_baidu;
	private RadioButton rd_gps;
	
	
//	private int choice_type = 0;
	private boolean isOpenLocation = false;
	private int locationType =0;
	private int timeInterval  =10;
	private int distanceInterval =10;
	
	private SwitchPreference mLocationPref;
	private ListPreference mLocationModePref;
	private EditTextPreference mReportTimerPref;
	private EditTextPreference mReportDistancePref;
	
//	private boolean isOpenServer = false;
//	private String serverip ="";
//	private int position_type = 0;
	
	private LocationConfig config = null;
	
	private TextView save_bt;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		//setContentView(R.layout.position_setting);
		
		addPreferencesFromResource(R.xml.pref_location);
//		sp = getSharedPreferences(LocalConfig.SHARED_POSITIONING_SETTING, Context.MODE_PRIVATE);
		initView();
		SysApplication app = ((SysApplication)getApplicationContext());
		Product product = app.getProduct();
		this.config = product.getLocationConfig();
		Log.d(THIS_FILE," onCreate config = " + config);
		initData(config);

	}

	private void initView() {
		
		mLocationPref = (SwitchPreference) findPreference("location_switch");
		mLocationPref.setOnPreferenceChangeListener(this);
		
		mLocationModePref = (ListPreference) findPreference("positioning_mode");
		mLocationModePref.setOnPreferenceChangeListener(this);
		
		mReportTimerPref = (EditTextPreference) findPreference("report_timer");
		mReportTimerPref.setOnPreferenceChangeListener(this);
		//mReportTimerPref.getEditText().setInputType(InputType.TYPE_CLASS_NUMBER);
		
		mReportDistancePref = (EditTextPreference) findPreference("report_distance");
		mReportDistancePref.setOnPreferenceChangeListener(this);
		//mReportDistancePref.getEditText().setInputType(InputType.TYPE_CLASS_NUMBER);
		

		/*isOpenLocationwitch =(Switch)findViewById(R.id.bt_switch);
		isOpenLocationwitch.setOnCheckedChangeListener(new OnCheckedChangeListener(){

			@Override
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
					if (isChecked) {  
						isOpenLocation = true;
	                } else {
	                	isOpenLocation = false;
	                }  
			}});
		
//		server_switch = (Switch) findViewById(R.id.bt_server_switch);
		
		timeIntervalTextView = (TextView)findViewById(R.id.id_positioning_interval);
		
		timeIntervalTextView.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {

					FragmentManager fragmentManager = getFragmentManager();
					InputTextDialog inputNameDialog = new InputTextDialog();
					inputNameDialog.setCancelable(false);
					inputNameDialog.setDialogTitle(getString(R.string.input_positioning_interval));
					inputNameDialog.setRid(R.id.id_positioning_interval);
					inputNameDialog.setOldValue(timeIntervalTextView.getText().toString());
					inputNameDialog.show(fragmentManager, "Input Dialog");
			}
		});

		distanceIntervalTextView = (TextView)findViewById(R.id.id_positioning_distance);
		distanceIntervalTextView.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {

					FragmentManager fragmentManager = getFragmentManager();
					InputTextDialog inputNameDialog = new InputTextDialog();
					inputNameDialog.setCancelable(false);
					inputNameDialog.setDialogTitle(getString(R.string.input_positioning_distance));
					inputNameDialog.setRid(R.id.id_positioning_distance);
					inputNameDialog.setOldValue(distanceIntervalTextView.getText().toString());
					inputNameDialog.show(fragmentManager, "Input Dialog");
			}
		});
		

		
		rd_baidu = (RadioButton)findViewById(R.id.rd_baidu);
		rd_gps = (RadioButton)findViewById(R.id.rd_gps);
		rd_gps.setEnabled(false);
		rd_baidu.setEnabled(false);
		
//		rg_position_choice = (RadioGroup)findViewById(R.id.rg_position_choice);
//		rd_sub = (RadioButton)findViewById(R.id.rd_sub);
//		rd_all = (RadioButton)findViewById(R.id.rd_all);
		
		
		rg_position_map = (RadioGroup)findViewById(R.id.rg_map);
		rg_position_map.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				
				if(checkedId == R.id.rd_baidu)
				{
					Log.d(THIS_FILE," baidu ");
					locationType = 1;
				}
				else if(checkedId == R.id.rd_gps)
				{
					Log.d(THIS_FILE," gps ");
					locationType = 0;
				}
				
			}
		});*/
		
//		rg_position_choice.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
//			
//			@Override
//			public void onCheckedChanged(RadioGroup group, int checkedId) {
//				
//				if(checkedId == R.id.rd_sub)
//				{
//					Log.d(THIS_FILE," sub ");
//					choice_type = 0;
//				}
//				else if(checkedId == R.id.rd_all)
//				{
//					Log.d(THIS_FILE," all ");
//					choice_type = 1;
//				}
//				
//			}
//		});


		
//		ly_server = (View)findViewById(R.id.layout_server_ip);
//		ly_server.setOnClickListener(new OnClickListener() {
//			
//			@Override
//			public void onClick(View v) {
//			
//				if(isOpenServer)
//				{
//					Intent intent = new Intent(CustomPositioningActivity.this,ServerSetting.class);
//					startActivityForResult(intent, Const.POSITIONING_REQCODE);
//				}
//			}
//		});
		
//		tv_positioning_server_ipport = (TextView)findViewById(R.id.tv_positioning_server_ipport);
//		server_switch.setOnCheckedChangeListener(new OnCheckedChangeListener() {
//			
//			@Override
//			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//				if (isChecked) {  
//					isOpenServer = true;
//					ly_server.setVisibility(View.VISIBLE);
//                } else {
//                	isOpenServer = false;
//                	ly_server.setVisibility(View.GONE);
//                }  
//			}
//		});
		
		
		/*save_bt = (TextView)findViewById(R.id.save_bt);
		save_bt.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) 
			{
				if(timeIntervalTextView.getText().toString().length()==0
						|| distanceIntervalTextView.getText().toString().length()==0)
				{
					CommDlg.showToast(CustomPositioningActivity.this, "不能为空");
					return;
				}
				if(config != null)
				{
					config.setOpenLocation(isOpenLocation,locationType);
					
					String val = timeIntervalTextView.getText().toString();
					int iVal = CheckData.parseInt(val);
					config.setTimeInterval(iVal);
					
					val = distanceIntervalTextView.getText().toString();
					iVal = CheckData.parseInt(val);
					config.setDistanceInterval(iVal);
				}
				CustomPositioningActivity.this.finish();
			}
		});*/
		
		

	}
	private void initData(LocationConfig config) 
	{
 		 isOpenLocation = config.isOpenLocation();
 		 if(isOpenLocationwitch!=null)
 		 {
 			mLocationPref.setChecked(isOpenLocation);
 		 }
 		 
 		 updateLocationMode();
		 
		 timeInterval  = config.getTimeInterval();
		 if(mReportTimerPref !=null)
		 {
			 mReportTimerPref.setText(String.valueOf(timeInterval));
			 mReportTimerPref.setSummary(String.valueOf(timeInterval));
		 }
		 
		 distanceInterval = config.getDistanceInterval();
		 if(mReportDistancePref !=null)
		 {
			 mReportDistancePref.setText(String.valueOf(distanceInterval));
			 mReportDistancePref.setSummary(String.valueOf(distanceInterval));
		 }
		 
//		 choice_type = sp.getInt(getString(R.string.pref_positioning_choice_switch), 0);
//		 if(rd_sub!=null && rd_all!=null)
//		 {
//			 if(choice_type==0)
//			 {
//				 rd_sub.setChecked(true);
//			 }
//			 else if(choice_type==1)
//			 {
//				 rd_all.setChecked(true);
//			 }
//		 }
		 
// 		 isOpenServer = sp.getBoolean(getString(R.string.pref_positioning_server_switch), false);
// 		 if(server_switch!=null)
// 		 {
// 			server_switch.setChecked(isOpenServer);
// 		 }

// 		 serverip = sp.getString(getString(R.string.pref_positioning_server_address),Const.DEFAULT_POSITIONING_IP+Const.ADDRESS_SPLIT+Const.DEFAULT_POSITIONING_PORT);
//   		 if(tv_positioning_server_ipport!=null)
//   		 {
//   	   		 tv_positioning_server_ipport.setText(serverip);
//   		 }
   		 
//    	 position_type = sp.getInt(getString(R.string.pref_positioning_type), 0);
   		 /*if(!isOpenLocation)
   		 {
   			isOpenLocationwitch.setEnabled(true);
   			rd_gps.setEnabled(false);
   			rd_baidu.setEnabled(false);
   			timeIntervalTextView.setEnabled(false);
   			distanceIntervalTextView.setEnabled(false);
//   			rd_sub.setEnabled(false);
//   			rd_all.setEnabled(false);
//   			server_switch.setEnabled(false);
//   			tv_positioning_server_ipport.setEnabled(false);
   		 }
   		 else
   		 {
    			isOpenLocationwitch.setEnabled(true);
       			rd_gps.setEnabled(true);
       			rd_baidu.setEnabled(true);
       			timeIntervalTextView.setEnabled(true);
       			distanceIntervalTextView.setEnabled(true);
//       			rd_sub.setEnabled(true);
//       			rd_all.setEnabled(true);
//       			server_switch.setEnabled(true);
//       			tv_positioning_server_ipport.setEnabled(true);
   		 }*/
   		 
	}
	
	private void updateLocationMode(){
		
		locationType =config.getLocationType();
		CharSequence[] values = mLocationModePref.getEntryValues();
		
		int index = -1;
		
		for(int i = 0; i < values.length; i++){
			if(locationType == Integer.parseInt((String) values[i])){
				index = i;
			}
		}
		
		if(index != -1){
			mLocationModePref.setValueIndex(index);
			mLocationModePref.setSummary((mLocationModePref.getEntries()[index]));
		}else{
			mLocationModePref.setSummary("");
		}
		
	}




//    @Override
//    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
//
//    	
////    	if(requestCode==Const.POSITIONING_REQCODE)
////    	{  		
//////    		serverip = sp.getString(Const.SHARED_PROP_POSITIONING_ADDRESS,Const.DEFAULT_POSITIONING_IP+":"+Const.DEFAULT_POSITIONING_PORT);
//////    		tv_positioning_server_ipport.setText(serverip);
////    	}
//    }
	
	/*@Override
	public void onFinishInputDialog(int Rid ,String inputText) {	
		
		int num = Integer.valueOf(inputText);
		
		if(Rid==R.id.id_positioning_interval)
		{
			timeIntervalTextView.setText(String.valueOf(num));
		}
		else if(Rid==R.id.id_positioning_distance)
		{
			distanceIntervalTextView.setText(String.valueOf(num));
		}
		
	}*/

	@Override
	public boolean onPreferenceClick(Preference preference) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean onPreferenceChange(Preference preference, Object newValue) {
		// TODO Auto-generated method stub
		if(newValue == null)
		{
			return false;
		}
		if("location_switch".equals(preference.getKey())){
			isOpenLocation = Boolean.parseBoolean(newValue.toString());
			if(config != null)
			{
				config.setOpenLocation(isOpenLocation,locationType);
			}
			return true;
		
		}else if("positioning_mode".equals(preference.getKey())){
			
			int mode = Integer.parseInt((String)newValue);
			locationType = mode;
			if(config != null)
			{
				config.setOpenLocation(isOpenLocation,locationType);
			}
			
			return true;
			
		}else if("report_timer".equals(preference.getKey())){
			
			if(newValue != null && !((String)newValue).trim().equals("")){
				int iVal = CheckData.parseInt((String)newValue);
				if(config != null){
					config.setTimeInterval(iVal);
					mReportTimerPref.setSummary((String)newValue);
					return true;
				}
			}
			
		}else if("report_distance".equals(preference.getKey())){
			int iVal = CheckData.parseInt((String)newValue);
			if(config != null){
				config.setDistanceInterval(iVal);
				mReportDistancePref.setSummary((String)newValue);
			}
			return true;
			
		}
		
		return false;
	}

}
